package com.shengyuan.manage_os.old.ying.erpm.tasks;

import android.content.Context;

import com.shengyuan.manage_os.R;
import com.shengyuan.manage_os.im.common.CCPAppManager;
import com.shengyuan.manage_os.im.common.utils.ToastUtil;
import com.shengyuan.manage_os.im.core.ClientUser;
import com.shengyuan.manage_os.im.ui.SDKCoreHelper;
import com.shengyuan.manage_os.old.ying.boce.http.HttpAuthException;
import com.shengyuan.manage_os.old.ying.boce.http.HttpException;
import com.shengyuan.manage_os.old.ying.boce.task.GenericTask;
import com.shengyuan.manage_os.old.ying.boce.task.TaskParams;
import com.shengyuan.manage_os.old.ying.boce.task.TaskResult;
import com.shengyuan.manage_os.old.ying.erpm.LoginActivity;
import com.shengyuan.manage_os.old.ying.erpm.MainApplication;
import com.shengyuan.manage_os.sqlite.SQLiteTools;
import com.yuntongxun.ecsdk.ECInitParams;

import oa.database.bean.Employee;

import static com.shengyuan.manage_os.old.ying.erpm.LoginActivity.pmUser;

/**
 * Created by Administrator on 2016/12/15.
 */

public 	class LoginTask extends GenericTask {
    public String Message ;
    private Context context;
    public LoginTask(Context context){
        this.context = context;
    }
    @Override
    protected TaskResult _doInBackground(TaskParams... params) {

        TaskParams param= params[0];
        try {
            String username = param.getString("username");
            String password = param.getString("password");
            pmUser = MainApplication.mApiErpm.login(username, password);
            if (pmUser!=null && pmUser.ID != 0) {
                pmUser.Password = password;
                Employee employee = SQLiteTools.queryAssignEmployeeByName(username);
                if (employee == null){
                    ToastUtil.showMessage("用户名不存在！");
                    return TaskResult.CANCELLED;
                }
                String id = Long.toString(employee.getUid());
                LoginActivity.pmUser.ID = Integer.parseInt(id);
                String appKey = "8a48b5514b75e1e1014b816085ec0580";
                String token = "d8d41f660dae11e5ac73ac853d9f54f2";
                ClientUser clientUser = new ClientUser(id);
                clientUser.setAppKey(appKey);
                clientUser.setAppToken(token);
                clientUser.setLoginAuthType(ECInitParams.LoginAuthType.NORMAL_AUTH);
                clientUser.setPassword("");
                CCPAppManager.setClientUser(clientUser);
                SDKCoreHelper.init(context, ECInitParams.LoginMode.FORCE_LOGIN);
                return TaskResult.OK;
            }else {
                pmUser = null;
                return TaskResult.CANCELLED;
            }
        } catch (HttpException e) {
            if (e instanceof HttpAuthException) {
                Message = context.getString(R.string.login_status_invalid_username_or_password);
            } else {
                Message = e.getMessage() +"\n"+ context.getString(R.string.login_status_network_or_connection_error);
            }
            return TaskResult.FAILED;
        }
    }

}
